<?php
class SP_Controller_Route
{
	protected static $last_query;
	
	public static function readConfig()
	{
		SP_Loader::loadPath(SP_Path::get('app_configs').SP_Config::get('system_app')->route_name);
	}

	protected static function moduleExists($name)
	{
		SP_Path::set('module', $name, 'app_modules');
		return is_file(SP_Path::get('module').SP_Config::get('system_app')->module_controller_name);
	}
	
	public static function getModule($name)
	{
		if( self::moduleExists($name) )
		{
			require_once(SP_Path::get('module').SP_Config::get('system_app')->module_controller_name);
			$class = SP_Common_Functions::splitSlashes($name);
			$class = 'Controller'.end($class);
			$module = new $class(SP_Path::get('module'));
			return $module;
		}
		return false;
	}
	
	public static function get404()
	{
		self::readConfig();
		$route_name=SP_Config::get('app')->get('route_404', 'page404');
		$route=SP_Routing_RouteCollector::get($route_name);
		if( !$route ) throw new Exception('404 route with name "'.$route_name.'" not found');
		$module_name=$route->getModule();
		$action_name=$route->getAction();
		if( empty($action_name) ) $action_name=SP_Config::get('app')->get('default_action', 'index');
		$module=self::getModule($module_name);
		if( $module )
		{
			if( !$module->hasAction($action_name) ) throw new Exception('404 action named "'.$action_name.'" in module "'.$module_name.'" not found');
			$module->setAction($action_name);
			return $module;
		}
		else
		{
			throw new Exception('404 module not found');
		}
	}
	
	public static function getLogin()
	{
		self::readConfig();
		$route_name=SP_Config::get('app')->get('route_login', 'pageLogin');
		$route=SP_Routing_RouteCollector::get($route_name);
		if( !$route ) throw new Exception('Login route with name "'.$route_name.'" not found');
		$module_name=$route->getModule();
		$action_name=$route->getAction();
		if( empty($action_name) ) $action_name=SP_Config::get('app')->get('default_action', 'index');
		$module=self::getModule($module_name);
		if( $module )
		{
			if( !$module->hasAction($action_name) ) throw new Exception('Login action named "'.$action_name.'" in module "'.$module_name.'" not found');
			$module->setAction($action_name);
			return $module;
		}
		else
		{
			throw new Exception('Login module not found');
		}
	}
	
	public static function get($uri=null)
	{
		self::readConfig();
		$router = new SP_Routing_Router;
		$router->setUri($uri);
		$route_name=$router->defineRoute();
		self::$last_query=$router->getUri();
		if( $route_name )
		{
			//var_dump($router);
			$module_name=$router->getModule();
			if( self::moduleExists($module_name) )
			{
				$module=self::getModule($module_name);
				$action_name=$router->getAction();
				if( $module->hasAction($action_name) )
				{
					$module->setAction($action_name);
					$vars=$router->getVars();
					unset($vars['module']);
					unset($vars['action']);
					$module->setRouteVars($vars);
					return $module;
				}
			}
		}
	}
	
}